package com.weavernorth.humanresource.regular;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

/**
 * @program: ecology_erdos
 * @description: 定时查询转正流程第二个节点
 * @author: zm
 * @create: 2020-01-06
 **/


public class RegularTimer extends BaseCronJob {
    public static Log log = LogFactory.getLog(RegularTimer.class.getName());

    @Override
    public void execute() {
        //基础类
        BaseBean bb = new BaseBean();
        //转正处理类
        RegularDeal regularDeal = new RegularDeal();
        //数据库操作
        RecordSet rs = new RecordSet();
        //获取转正流程的workflowid
        String strWorkflowid = Util.null2String(bb.getPropValue("regular", "regularWfid"));
        if (!"".equals(strWorkflowid)) {
            String[] strWOrkflowidArr = strWorkflowid.split(",");
            //转正流程第二个节点nodeid
            String strNodeid = Util.null2String(bb.getPropValue("regular", "regularNodeid"));
            String[] strArrNodeid=strNodeid.split(",");
            for (int i = 0; i < strWOrkflowidArr.length; i++) {


                //获取转正表名
                rs.execute("select tablename  from workflow_bill where id = (select formid from workflow_base where id=" + strWOrkflowidArr[i] + " and isbill=1)");
                rs.next();
                String strTable = Util.null2String(rs.getString("tablename"));
                log.info("当前表名:" + strTable);


                //转正流程超时提交时间
                String strDay = Util.null2String(bb.getPropValue("regular", "regularDay"));
                //查询转正流程第二个节点30个自然日未处理的流程
                String strSql = "select requestid,userid from workflow_currentoperator where to_char(to_date(receivedate,'yyyy-mm-dd') + interval '" + strDay + "' day(4), 'yyyy-mm-dd')=(select to_char(sysdate, 'yyyy-mm-dd') from dual)" +
                        " and operatedate is  null and isremark=0 and workflowid=? and nodeid=? ";
                log.info("获取转正未提交的sql:" + strSql + ",workflowid:" +  strWOrkflowidArr[i] + ",nodeid:" + strArrNodeid[i]);
                rs.executeQuery(strSql, strWOrkflowidArr[i], strArrNodeid[i]);
                while (rs.next()) {
                    //获取流程id
                    String strRequestid = Util.null2String(rs.getString("requestid"));
                    //当前操作人
                    int strUserid = Util.getIntValue(rs.getString("userid"));
                    //自动提交流程
                    String strResult = regularDeal.getRegular(strRequestid, strUserid);
                    log.info("自动提交流程返回结果:" + strResult);
                    //提交成功后推送SAP
                    if (!"failed".equals(strResult)) {
                        regularDeal.sendSAP(strRequestid, strTable);
                    }
                }

            }

        }
    }
}

